#
# CPL Assignment 2
# Tim Caljé, Jeroen De Raedt & Jorick Van Aken
#
class Route
  
    def initialize(flights)
      @flights = flights 
      # check for route consistency
      raise "Invalid route" if !isValid()
    end       
    
    def flights
      @flights
    end    
    
    def to_s()
      "[Route #{flights.first.departure} to #{flights.last.arrival} (#{flights.length} flights, duration: #{totalTime/60} mins.)] \n #{getFlightsString()}"      
    end
    
    def hasAvailableSeats(type, amount)
      tmpFlights = flights.find_all{|flight| flight.nbFreeSeats(type) >= amount}
      return tmpFlights.length == flights.length
    end
    
    def compare(other, orderType, amountPersons, seatType)
      case orderType
      when FlightsOrder::CHEAPEST
        return self.totalPrice(amountPersons, seatType) > other.totalPrice(amountPersons, seatType) ? 1 : -1
      when FlightsOrder::SHORTEST
        return self.totalTime > other.totalTime ? 1 : -1
      else
        return 1
      end
    end
    
    def totalPrice(amountPersons, seatType)
      case seatType
      when SeatType::BUSINESS
        return self.totalBusinessPrice()
      when SeatType::FIRST_CLASS
        return self.totalFirstClassPrice()
      when SeatType::ECONOMY
        return self.totalEconomyPrice()
      else      # no seattype specified
        return availablePrice(amountPersons)
      end
    end
    
    def totalTime
      firstDepartureTime = flights.first.departureTime
      lastArrivalTime = flights.last.arrivalTime
      return lastArrivalTime - firstDepartureTime
    end
    
    def totalFirstClassPrice
      totalPrice = 0.0
      flights.each do |flight|
        totalPrice += flight.priceFirstClass
      end
      return totalPrice
    end
    
    def totalBusinessPrice
      totalPrice = 0.0
      flights.each do |flight|
        totalPrice += flight.priceBusinessClass
      end
      return totalPrice
    end
    
    def totalEconomyPrice
      totalPrice = 0.0
      flights.each do |flight|
        totalPrice += flight.priceEconomy
      end
      return totalPrice
    end
    
    private
    def getFlightsString()
      result = flights[0].to_s
      for i in 1...flights.length
        result = "#{result} \n #{flights[i].to_s}"
      end      
      return result
    end
    
    def isValid()
      if flights.length == 0
        return false #Empty route 
      end
      first = true
      previous = nil
      flights.each do |flight|
         if first == true
           previous = flight
           first = false
         else
           if !flight.departure.equal?(previous.arrival)
             return false # departures - arrivals don't match
           elsif flight.departureTime < previous.arrivalTime
             return false # departures-time - arrivals-time don't follow-up
           end
         end
      end
      return true  
    end
    
    # this method determines the price of the flights if no seattype is specified
    # it takes into account whether the given amount of seats are still availble in 1 type of seats
    def availablePrice(amountPersons)
      totalPrice = 0.0
      
      flights.each do |flight|
        totalPrice += flight.placePersons(amountPersons)
      end
      return totalPrice
    end
  
end